import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
  listtiku = '/h5/sys/exam_question_bank/list',
  list = '/h5/sys/exam_pwd/list',
  save='/h5/sys/exam_pwd/add',
  edit='/h5/sys/exam_pwd/edit',
  deleteOne = '/h5/sys/exam_pwd/delete',
  deleteBatch = '/h5/sys/exam_pwd/deleteBatch',
  importExcel = '/h5/sys/exam_pwd/importExcel',
  exportXls = '/h5/sys/exam_pwd/exportXls',
  getpwd = '/h5/sys/exam_pwd/getPassword',
}
/**
 * 题库列表接口
 * @param params
 */
export const listtiku = (params) => defHttp.get({ url: Api.listtiku, params }, {joinTime: false});
/**
 * 获取口令
 * 
 */
export const getpwd = (params) => defHttp.get({ url: Api.getpwd, params }, {joinTime: false});
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = async(params) =>{
  let data = await defHttp.get({ url: Api.list, params })
    // 遍历records数组
  data.records.forEach(record => {
    if (record.timeList) {
        try {
            // 解析timeList JSON字符串
            const timeList = JSON.parse(record.timeList);
            
            // // 假设timeList是按时间顺序排列的，取最后一个元素即为最近的一次
            const latestTimeEntry1 = timeList[timeList.length - 1];
            const latestTimeEntry = timeList[0];
            // // 更新外层的startTime和stopTime
            record.startTime = latestTimeEntry.startTime;
            record.stopTime = latestTimeEntry.stopTime;
            record['startTimeTwo'] = latestTimeEntry1.startTime
            record['stopTimeTwo'] = latestTimeEntry1.stopTime
        } catch (error) {
            console.error('Error parsing timeList:', error);
        }
    }
  });
  console.log(data,"datas")
  return  data
};

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 * @param isUpdate
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
}
